package com.example.neusoft.widgtetest;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv;
    private EditText et;
    private TextView tv;
    private CheckBox cblan, cbzu, cbyou;
    private RadioGroup rg;
    private RadioButton rbtnnan, rbtnnv;
    private String msg = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        iv = (ImageView) findViewById(R.id.imageView);
        et = (EditText) findViewById(R.id.editText);
        tv = (TextView) findViewById(R.id.textView);
        cblan = (CheckBox) findViewById(R.id.checkBox);
        cbzu = (CheckBox) findViewById(R.id.checkBox2);
        cbyou = (CheckBox) findViewById(R.id.checkBox3);
        rbtnnan = (RadioButton) findViewById(R.id.radioButton);
        rbtnnv = (RadioButton) findViewById(R.id.radioButton2);
        rg = (RadioGroup) findViewById(R.id.radiogroup);
        rbtnnan.setChecked(true);

        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.imageButton).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);


        cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b)
                    msg += "篮球+";
                else
                    msg = msg.replace("篮球+", "");
                tv.setText(msg);
            }
        });
        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b)
                    msg += "足球+";
                else
                    msg = msg.replace("足球+", "");
                tv.setText(msg);
            }
        });
        cbyou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b)
                    msg += "游泳+";
                else
                    msg = msg.replace("游泳+", "");
                tv.setText(msg);
            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.radioButton:
                        tv.setText("男");
                        break;
                    case R.id.radioButton2:
                        tv.setText("女");
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                String str = et.getText().toString();
                tv.setText(str);
                break;
            case R.id.button2:
                iv.setImageResource(R.drawable.google);
                break;
            case R.id.imageButton:
                Intent intent = new Intent(MainActivity.this, NewActivity.class);
                intent.putExtra("para1", et.getText().toString());
                intent.putExtra("para2", 3);
                startActivity(intent);
                break;
            case R.id.button3:
                String str2 = "";
                if (cblan.isChecked()) str2 += "篮球+";
                if (cbzu.isChecked()) str2 += "足球+";
                if (cbyou.isChecked()) str2 += "游泳+";
                tv.setText(str2);
                break;
            case R.id.button5:
                if (rbtnnan.isChecked()) tv.setText("男");
                if (rbtnnv.isChecked()) tv.setText("女");
                break;
        }

    }
}
